package com.hyz.testsdcardhtml;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import java.io.IOException;

import static com.hyz.testsdcardhtml.App.DEFAULT_FONT_PATH;
import static com.hyz.testsdcardhtml.App.KAI_FONT_PATH;
import static com.hyz.testsdcardhtml.App.ROOT_FONT_PATH;
import static com.hyz.testsdcardhtml.App.SON_FONT_PATH;
import static com.hyz.testsdcardhtml.App.YOU_FONT_PATH;
import static com.hyz.testsdcardhtml.App.YZQS_FONT_PATH;

public class MainActivity extends Activity implements View.OnClickListener {

    WebView webView;
    Button btn_son;
    Button btn_kai;
    Button btn_you;
    Button btn_yzqs;
    Button btn_old;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webview);
        btn_son = findViewById(R.id.btn_son);
        btn_kai = findViewById(R.id.btn_kai);
        btn_you = findViewById(R.id.btn_you);
        btn_yzqs = findViewById(R.id.btn_yzqs);
        btn_old = findViewById(R.id.btn_old);


        btn_son.setOnClickListener(this);
        btn_kai.setOnClickListener(this);
        btn_you.setOnClickListener(this);
        btn_yzqs.setOnClickListener(this);
        btn_old.setOnClickListener(this);

        releaseFonts();

        webView.loadUrl("file:///storage/emulated/0/art/hello.html");
    }

    private void releaseFonts() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ArmFileUtils.createOrExistsDir(ROOT_FONT_PATH);
                if (ArmFileUtils.isFileExists(SON_FONT_PATH) &&
                        ArmFileUtils.isFileExists(KAI_FONT_PATH) &&
                        ArmFileUtils.isFileExists(YZQS_FONT_PATH) &&
                        ArmFileUtils.isFileExists(YOU_FONT_PATH)) {
                    //如果这3个字体都存在，则不用复制了。否则就要复制
                    Log.d("MainActivity", "这4个字体都存在");
                } else {
                    Log.d("MainActivity", "字体异常删除后，重新复制");
                    long time1 = System.currentTimeMillis();
                    ArmFileUtils.deleteFilesInDir(ROOT_FONT_PATH);
                    try {
                        AssetUtils.doCopy(MainActivity.this, "fonts2", ROOT_FONT_PATH);
                        long time2 = System.currentTimeMillis();
                        Log.d("MainActivity", "复制耗时 ===" + (time2 - time1));
                    } catch (IOException e) {
                        Log.d("MainActivity", "复制字体异常了");
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }


    @Override
    public void onClick(View v) {

        if (ArmFileUtils.isFileExists(DEFAULT_FONT_PATH)) {
            Log.d("MainActivity", "删除旧字体成功");
            ArmFileUtils.deleteFile(DEFAULT_FONT_PATH);
        }

        switch (v.getId()) {
            case R.id.btn_son:
                if (ArmFileUtils.isFileExists(SON_FONT_PATH)) {
                    Log.d("MainActivity", "宋体，复制更名新字体成功");
                    ArmFileUtils.copyFile(SON_FONT_PATH, DEFAULT_FONT_PATH);
                    restartApp();
                }
                break;

            case R.id.btn_kai:
                if (ArmFileUtils.isFileExists(KAI_FONT_PATH)) {
                    Log.d("MainActivity", "楷体，复制更名新字体成功");
                    ArmFileUtils.copyFile(KAI_FONT_PATH, DEFAULT_FONT_PATH);
                    restartApp();
                }
                break;

            case R.id.btn_you:
                if (ArmFileUtils.isFileExists(YOU_FONT_PATH)) {
                    Log.d("MainActivity", "幼圆，复制更名新字体成功");
                    ArmFileUtils.copyFile(YOU_FONT_PATH, DEFAULT_FONT_PATH);
                    restartApp();
                }
                break;
            case R.id.btn_yzqs:
                if (ArmFileUtils.isFileExists(YZQS_FONT_PATH)) {
                    Log.d("MainActivity", "一纸情书，复制更名新字体成功");
                    ArmFileUtils.copyFile(YZQS_FONT_PATH, DEFAULT_FONT_PATH);
                    restartApp();
                }
                break;
            case R.id.btn_old:
                Log.d("MainActivity", "恢复默认字体成功");
                restartApp();
                break;
        }

    }

    private void restartApp() {
        Intent intent = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
        PendingIntent restartIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用
        appExit();
    }


    public void appExit() {
        try {
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
